﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Communication;
using Global;
using System.Text.RegularExpressions;
using System.ComponentModel;

namespace Clipper.EngineerWindow
{
    /// <summary>
    /// UserAdjustment.xaml 的交互逻辑
    /// </summary>
    public partial class UserAdjustment : UserControl
    {
        public UserAdjustment()
        {
            InitializeComponent();
        }

        #region 变量初始化
        const string strEnd = "\r";
        const string DC = "DC";//限位，修改步数
        const string DE = "DE";//定位，修改减速度
        const string DI = "DI";//微调吸液量，修改步数
        const string SA = "SA";//保存参数
        const string VE="VE";//设置速度

        BLL.Adjustment mAdjustmentBll = null;
        List<Model.Adjustment> mAdjustList = null;

        //滑轨微调电机
        const string motorMoveA = "1";
        const string motorMoveB = "4";
        const string motorMoveC = "1";
        const string motorMoveD = "4";
        const string motorMoveE = "3";
        const string motorDetect = "1";//微调电机
       
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            armSA.CanID = 0x41;
            armSB.CanID = 0x43;
            armR1A.CanID = 0x42;
            armR1B.CanID = 0x44;
            armR2A.CanID = 0x53;
            armR2B.CanID = 0x54;
            Can.SendAdjustEvent += new Can.SendDelegate(Can_SendAdjustEvent);
            ShowAdjustData();
        }
        //显示已保存参数
        private void ShowAdjustData()
        {
            BackgroundWorker backWorker = new BackgroundWorker();
            backWorker.DoWork += (ss, ee) =>
                {
                    mAdjustmentBll = new BLL.Adjustment();
                    mAdjustList = mAdjustmentBll.GetModelList("");
                };
            backWorker.RunWorkerCompleted += (ss, ee) =>
                {
                    foreach (Model.Adjustment model in mAdjustList)
                    {
                        string target = model.CanIDCmd.Remove(0, model.remark.Length);
                        short canID = short.Parse(model.remark);
                        switch (canID)
                        {
                            #region SA_MoveA_ID
                            case Can.SA_MoveA_ID:
                                switch (target)
                                {
                                    case motorMoveA + DE:
                                        txtAA.Text = model.Value.ToString();
                                        break;
                                    case motorMoveA + VE:
                                        txtAASpeed.Text = model.Value.ToString();
                                        break;
                                }
                                break;
                            #endregion

                            #region MoveB_MoveC_Substrate
                            case Can.MoveB_MoveC_Substrate_ID:
                                switch (target)
                                {
                                    case motorMoveB + DE:
                                        txtAB.Text = model.Value.ToString();
                                        break;
                                    case motorMoveC + DE:
                                        txtAC.Text = model.Value.ToString();
                                        break;
                                    case motorMoveB + VE:
                                        txtABSpeed.Text = model.Value.ToString();
                                        break;
                                    case motorMoveC + VE:
                                        txtACSpeed.Text = model.Value.ToString();
                                        break;
                                    case "3DE":
                                        txtACArm.Text = model.Value.ToString();
                                        break;
                                    case "2DI":
                                        txtACArmDI.Text = model.Value.ToString();
                                        break;
                                }
                                break;
                            #endregion

                            #region MoveD_MoveE
                            case Can.MoveD_MoveE_ID:
                                switch (target)
                                {
                                    case motorMoveD + DE:
                                        txtAD.Text = model.Value.ToString();
                                        break;
                                    case motorMoveE + DE:
                                        txtAE.Text = model.Value.ToString();
                                        break;
                                    case motorMoveD + VE:
                                        txtADSpeed.Text = model.Value.ToString();
                                        break;
                                    case motorMoveE + VE:
                                        txtAESpeed.Text = model.Value.ToString();
                                        break;
                                }
                                break;
                            #endregion

                            #region R1A_Kit1_ID
                            case Can.R1A_Kit1_ID:
                                switch (target)
                                {
                                    case "1DE":
                                        txtKit1.Text = model.Value.ToString();
                                        break;
                                }
                                break;
                            #endregion

                            #region R2A_Kit2_ID
                            case Can.R2A_Kit2_ID:
                                switch (target)
                                {
                                    case "1DE":
                                        txtKit2.Text = model.Value.ToString();
                                        break;
                                }
                                break;
                            #endregion

                            #region Incubation
                            case Can.Incubation_ID:
                                switch (target)
                                {
                                    case "1PA":
                                        po1.Number = model.Value.Value;
                                        break;
                                    case "1PB":
                                        po2.Number = model.Value.Value;
                                        break;
                                    case "1PC":
                                        po3.Number = model.Value.Value;
                                        break;
                                    case "1PD":
                                        po4.Number = model.Value.Value;
                                        break;
                                    case "1PE":
                                        po5.Number = model.Value.Value;
                                        break;
                                    case "1PF":
                                        po6.Number = model.Value.Value;
                                        break;
                                    case "1PG":
                                        po7.Number = model.Value.Value;
                                        break;
                                    case "1PH":
                                        po8.Number = model.Value.Value;
                                        break;
                                    case "1PI":
                                        po9.Number = model.Value.Value;
                                        break;
                                    case "1PJ":
                                        po10.Number = model.Value.Value;
                                        break;
                                    case "1PK":
                                        po11.Number = model.Value.Value;
                                        break;
                                    case "1PL":
                                        po12.Number = model.Value.Value;
                                        break;
                                }
                                break;
                            #endregion

                            #region Detect
                            case Can.Detect_ID:
                                switch (target)
                                {
                                    case motorDetect+DE:
                                        txtDetectDE.Number = model.Value.Value;
                                        break;
                                }
                                break;
                            #endregion
                        }
                    }
                };
            backWorker.RunWorkerAsync();
            
        }
        #endregion

        #region 微调

        private void Adjust_Click(object sender, RoutedEventArgs e)
        {
            string btnName = ((Button)sender).Name;
            switch (btnName)
            {
                #region 滑轨微调
                case "btnAA":
                    Can.SendCan(Can.SA_MoveA_ID, motorMoveA + DE + txtAA.Text + strEnd);
                    break;
                case "btnAB":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, motorMoveB + DE + txtAB.Text + strEnd);
                    break;
                case "btnAC":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, motorMoveC + DE + txtAC.Text + strEnd);
                    break;
                case "btnAD":
                    Can.SendCan(Can.MoveD_MoveE_ID, motorMoveD + DE + txtAD.Text + strEnd);
                    break;
                case "btnAE":
                    Can.SendCan(Can.MoveD_MoveE_ID, motorMoveE + DE + txtAE.Text + strEnd);
                    break;
                case "btnAASpeed":
                    Can.SendCan(Can.SA_MoveA_ID, motorMoveA + VE + txtAASpeed.Text + strEnd);
                    break;
                case "btnABSpeed":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, motorMoveB + VE + txtABSpeed.Text + strEnd);
                    break;
                case "btnACSpeed":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, motorMoveC + VE + txtACSpeed.Text + strEnd);
                    break;
                case "btnADSpeed":
                    Can.SendCan(Can.MoveD_MoveE_ID, motorMoveD + VE + txtADSpeed.Text + strEnd);
                    break;
                case "btnAESpeed":
                    Can.SendCan(Can.MoveD_MoveE_ID, motorMoveE + VE + txtAESpeed.Text + strEnd);
                    break;
                #endregion

                #region 试剂盘、底物臂微调
                case "btnKit1":
                    Can.SendCan(Can.R1A_Kit1_ID, 1 + DE + txtKit1.Text + strEnd);
                    break;
                case "btnKit2":
                    Can.SendCan(Can.R2A_Kit2_ID, 1 + DE + txtKit2.Text + strEnd);
                    break;
                case "btnACArm":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, 3 + DE + txtACArm.Text + strEnd);
                    break;
                case "btnACArmDI":
                    Can.SendCan(Can.MoveB_MoveC_Substrate_ID, 2 + DI + txtACArmDI.Text + strEnd);
                    break;
                #endregion
                
                    //检测仓
                case "btnDetectDE":
                    Can.SendCan(Can.Detect_ID, motorDetect + DE + txtDetectDE.Text + strEnd);
                    break;

            }
        }
        void Can_SendAdjustEvent(short canID, string command)
        {
            if (command.Length > 5)//判断微调参数
            {
                switch (canID)
                {
                    case Can.SA_MoveA_ID:
                        AdjustDataCache(canID, command, AdjustProvider.SA_MoveA);
                        break;
                    case Can.SB_CupIn_ID:
                        AdjustDataCache(canID, command, AdjustProvider.SB_CupIn);
                        break;
                    case Can.R1A_Kit1_ID:
                        AdjustDataCache(canID, command, AdjustProvider.R1A_Kit1);
                        break;
                    case Can.R1B_ID:
                        AdjustDataCache(canID, command, AdjustProvider.R1B);
                        break;
                    case Can.R2A_Kit2_ID:
                        AdjustDataCache(canID, command, AdjustProvider.R2A_Kit2);
                        break;
                    case Can.R2B_ID:
                        AdjustDataCache(canID, command, AdjustProvider.R2B);
                        break;
                    case Can.MoveB_MoveC_Substrate_ID:
                        AdjustDataCache(canID, command, AdjustProvider.MoveB_MoveC_Substrate);
                        break;
                    case Can.MoveD_MoveE_ID:
                        AdjustDataCache(canID, command, AdjustProvider.MoveD_MoveE);
                        break;
                    case Can.Incubation_ID:
                        AdjustDataCache(canID, command, AdjustProvider.Incubation);
                        break;
                    case Can.Detect_ID:
                        AdjustDataCache(canID, command, AdjustProvider.Detect);
                        break;
                }
            }
        }
        private static void AdjustDataCache(Int16 canID, string command, Dictionary<string, Model.Adjustment> dataList)
        {

            string dataKey = canID + command.Substring(0, 3);
            int dataValue = int.Parse(command.Substring(3, command.Length - 5));
            if (dataList.ContainsKey(dataKey))
            {
                dataList[dataKey].Value = dataValue;
            }
            else
            {
                Model.Adjustment model = new Model.Adjustment()
                {
                    CanIDCmd = dataKey,
                    Value = dataValue,
                    remark = canID.ToString()
                };
                dataList.Add(dataKey, model);
            }
        }

        #endregion

        #region 参数缓存和保存

        //保存微调参数到下位机
        private void Save_Click(object sender, RoutedEventArgs e)
        {
             string btnName = ((Button)sender).Name;
            string strSaveCmd="1SA/r";
             switch (btnName)
             {
                 case "btnSave41":
                     Can.SendCan(Can.SA_MoveA_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.SA_MoveA_ID);
                     break;
                 case "btnSave42":
                     Can.SendCan(Can.R1A_Kit1_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.R1A_Kit1_ID);
                     break;
                 case "btnSave43":
                     Can.SendCan(Can.SB_CupIn_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.SB_CupIn_ID);
                     break;
                 case "btnSave44":
                     Can.SendCan(Can.R1B_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.R1B_ID);
                     break;
                 case "btnSave51":
                     Can.SendCan(Can.MoveD_MoveE_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.MoveD_MoveE_ID);
                     break;
                 case "btnSave52":
                     Can.SendCan(Can.MoveB_MoveC_Substrate_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.MoveB_MoveC_Substrate_ID);
                     break;
                 case "btnSave53":
                     Can.SendCan(Can.R2A_Kit2_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.R2A_Kit2_ID);
                     break;
                 case "btnSave54":
                     Can.SendCan(Can.R2B_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.R2B_ID);
                     break;
                 case "btnSave61":
                     Can.SendCan(Can.Incubation_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.Incubation_ID);
                     break;
                 case "btnSave71":
                     Can.SendCan(Can.Detect_ID, strSaveCmd);
                     SwitchSaveAdjust(Can.Detect_ID);
                     break;
             }
            
        } 
        //保存缓存中的数据到数据库
        private void SwitchSaveAdjust(Int16 canID)
        {
            switch (canID)
            {
                case Can.SA_MoveA_ID:
                    SaveAdjust(AdjustProvider.SA_MoveA);
                    break;
                case Can.SB_CupIn_ID:
                    SaveAdjust(AdjustProvider.SB_CupIn);
                    break;
                case Can.R1A_Kit1_ID:
                    SaveAdjust(AdjustProvider.R1A_Kit1);
                    break;
                case Can.R1B_ID:
                    SaveAdjust(AdjustProvider.R1B);
                    break;
                case Can.R2A_Kit2_ID:
                    SaveAdjust(AdjustProvider.R2A_Kit2);
                    break;
                case Can.R2B_ID:
                    SaveAdjust(AdjustProvider.R2B);
                    break;
                case Can.MoveB_MoveC_Substrate_ID:
                    SaveAdjust(AdjustProvider.MoveB_MoveC_Substrate);
                    break;
                case Can.MoveD_MoveE_ID:
                    SaveAdjust( AdjustProvider.MoveD_MoveE);
                    break;
                case Can.Incubation_ID:
                    SaveAdjust(AdjustProvider.Incubation);
                    break;
                case Can.Detect_ID:
                    SaveAdjust(AdjustProvider.Detect);
                    break;
            }
        }
        private void SaveAdjust(Dictionary<string, Model.Adjustment> dataList)
        {
            foreach(Model.Adjustment model in  dataList.Values)
            {
                mAdjustmentBll.AddOrReplace(model);
            }
        }

        #endregion

        private void TabControl_Unloaded(object sender, RoutedEventArgs e)
        {
            Can.SendAdjustEvent -= new Can.SendDelegate(Can_SendAdjustEvent);
        }

    }
}
